package com.jn.exam.oss.utils;

import sun.misc.BASE64Decoder;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 江南大学1033190417
 * @date 2022/1/12 22:35
 */
public class Base64ToImg {

    /**
     * base64转jpg图片
     *
     * @param imgStr
     * @param imgFilePath
     * @return
     * @throws IOException
     */
    public static boolean base64ToImg(String imgStr, String imgFilePath) throws IOException {
        if (imgStr == null) {//图片数据为空
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        //base64解码
        byte[] bytes = decoder.decodeBuffer(imgStr);
        for (int i=0;i< bytes.length;++i){
            if (bytes[i]<0){//调整异常数据
                bytes[i]+=256;
            }
        }
        //生成JPG图片
        OutputStream out=new FileOutputStream(imgFilePath);
        out.write(bytes);
        out.flush();
        out.close();
        return true;
    }

}
